let App = getApp();
Page({
  data: {
    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    wxapp:[],
    newlist:[],
    randomlist: [],
    banner:[],
    page:1,
    category:[],
    selectId:0
  },
  onLoad: function (options) {
    //页面启动后 调取首页的数据
    let that = this;
    App.getStorageSyncwxapp(function (ret) {
      that.setData({
        wxapp: ret
      });
      wx.setNavigationBarTitle({
        title: ret.LiteName
      });
    });
  },
  onShow: function () {
    //这里获得最近的商品数据 随机商品数据
    let that = this;
    //获取当前时间戳
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为：" + timestamp);
    //获取缓存时间
    var cacheIndextime = wx.getStorageSync('cacheIndextime');
    console.log("缓存时间戳为：" + cacheIndextime);
    if(cacheIndextime === '' || cacheIndextime < timestamp){
      App._get('index/index', {page:that.data.page}, function (result) {
        that.setData({
        banner: result.data.bannerlist,
        category: result.data.categorydata,
        page:1
      });
      wx.setStorageSync('banner', result.data.bannerlist);
      wx.setStorageSync('category', result.data.categorydata);
      wx.setStorageSync('cacheIndextime',timestamp+3600);
    });
    }else{
      var category = wx.getStorageSync('category');
      var banner = wx.getStorageSync('banner');
      that.setData({
        category:category,
        banner:banner
      });
    }
    that.setData({
      randomlist:[]
    })  
    App._post('index/randGoods',{page:that.data.page,type:that.data.selectId},function(res){
      if(res.code === 1){
        if(res.data.Randomlist.length === 0){
          // that.setData({
          //   text:"已经到底了"
          //  }) 
        
        }else{
          that.setData({
            randomlist:that.data.randomlist.concat(res.data.Randomlist)
          
          })  
        }
      }
    });
  },
   /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    //获取缓存时间
    var page;
    page = Number(that.data.page) + 1; 
    App._post('index/randGoods',{page:page,type:that.data.selectId},function(res){
      if(res.code === 1){
        if(res.data.Randomlist.length === 0){
          // that.setData({
          //   text:"已经到底了"
          //  }) 
          
        }else{
          that.setData({
            randomlist:that.data.randomlist.concat(res.data.Randomlist),
            page:page,
          })  
        }
      }
    });
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log('target',res)
    }
    return {
      title: "万步同城",
      desc: "哒哒哒哒哒哒多",
      path: "/pages/index/index"
    };
  },
  gotoSs:function(){
    // wx.switchTab({
      wx.navigateTo({
        url: '../category/index',
      })
    //   url: '../category/index'
    // })
  },
  selectCate:function(event){
    let that = this;
    that.setData({
      selectId:event.target.dataset.id,
      page:1,
      randomlist:[]
    });
    // var page;
    // page = Number(that.data.page); 
    App._post('index/randGoods',{page:1,type:that.data.selectId},function(res){
      if(res.code === 1){
        if(res.data.Randomlist.length === 0){
          // that.setData({
          //   text:"已经到底了"
          //  }) 
          
        }else{
          that.setData({
            randomlist:that.data.randomlist.concat(res.data.Randomlist)
          })  
        }
      }
    });
  }
})